class Property:

    def __init__(self, name, type, value):
        self.name_ = name
        self.type_ = type
        self.value_ = value

    def write(self, output):
        output.write("<PROPERTY")
        output.write(" ")
        
        output.write("NAME")
        output.write("=")
        output.write(" ")
        output.write("\"")
        output.write(str(self.name_))
        output.write("\"")
        output.write(" ")

        output.write("TYPE")
        output.write("=")
        output.write(" ")
        output.write("\"")
        output.write(str(self.type_))
        output.write("\"")
        output.write(" ")

        output.write("VALUE")
        output.write("=")
        output.write(" ")
        output.write("\"")
        output.write(str(self.value_))
        output.write("\"")
        output.write(" ")

        output.write("/>")
        output.write("\n")
        
class Vertex:
    
    def __init__(self, id):
        self.id_ = id
        self.properties = {}

    def add_property(self, property):
        self.properties[property.name_] = property

    def write(self, output):
        output.write("<VERTEX")
        output.write(" ")
        output.write("IDENTIFIER")
        output.write("=")
        output.write("\"")
        output.write(str(self.id_))
        output.write("\"")
        output.write(">")
        output.write("\n")

        for property_name in self.properties.keys():
            self.properties[property_name].write(output)

        output.write("</VERTEX>")
        output.write("\n")

class Edge:
    
    def __init__(self, id, directed, vertex1, vertex2):
        self.id_ = id
        self.directed_ = directed
        self.vertex1_ = vertex1
        self.vertex2_ = vertex2
        self.properties = {}

    def add_property(self, property):
        self.properties[property.name_] = property

    def write(self, output):
        output.write("<EDGE")
        output.write(" ")
        output.write("IDENTIFIER")
        output.write("=")
        output.write("\"")
        output.write(str(self.id_))
        output.write("\"")

        output.write(" ")

        output.write("DIRECTED")
        output.write("=")
        output.write("\"")
        output.write(str(self.directed_))
        output.write("\"")

        output.write(" ")

        output.write("VERTEX1")
        output.write("=")
        output.write("\"")
        output.write(str(self.vertex1_))
        output.write("\"")

        output.write(" ")

        output.write("VERTEX2")
        output.write("=")
        output.write("\"")
        output.write(str(self.vertex2_))
        output.write("\"")

        output.write(">")
        output.write("\n")

        for property_name in self.properties.keys():
            self.properties[property_name].write(output)

        output.write("</EDGE>")
        output.write("\n")
        
        
class Graph:    
    def __init__(self, id):
        self.id_ = id
        self.properties = {}
        self.vertices = {}
        self.vertex_count = 0
        self.edges = {}
        self.edge_count = 0
        
    def add_property(self, property):
        self.properties[property.name_] = property

    def add_vertex(self, vertex):
        self.vertices[vertex.id_] = vertex
        self.vertex_count += 1
    
    def add_edge(self, edge):
        self.edges[edge.id_] = edge
        self.edge_count += 1

    def add_vertex_property(self, vertex_id, property):
        self.vertices[vertex_id].add_property(property)

    def add_edge_property(self, edge_id, property):
        self.edges[edge_id].add_property(property)

    def write(self, output):
        output.write("<GRAPH")
        output.write(" ")
        output.write("IDENTIFIER")
        output.write("=")
        output.write("\"")
        output.write(str(self.id_))
        output.write("\"")
        output.write(">")
        output.write("\n")

        for property_name in self.properties.keys():
            self.properties[property_name].write(output)

        for vertex_id in self.vertices.keys():
            (self.vertices[vertex_id]).write(output)

        for edge_id in self.edges.keys():
            (self.edges[edge_id]).write(output)

        output.write("</GRAPH>")
        output.write("\n")
    
class GMRML:

    def __init__(self):
        self.graphs = {}
        self.graph_counter = 0

    def add_graph(self, graph):
        self.graphs[graph.id_] = graph
        self.graph_counter += 1

    def add_graph_property(self, graph_id, property):
        self.graphs[graph_id].add_property(property)

    def add_vertex(self, graph_id, vertex):
        self.graphs[graph_id].add_vertex(vertex)

    def add_edge(self, graph_id, edge):
        self.graphs[graph_id].add_edge(edge)

    def add_vertex_property(self, graph_id, vertex_id, property):
        self.graphs[graph_id].add_vertex_property(vertex_id, property)

    def add_edge_property(self, graph_id, edge_id, property):
        self.graphs[graph_id].add_edge_property(edge_id, property)        

    def write(self, output):
        output.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
        output.write("\n")
        output.write("<!DOCTYPE GMRML SYSTEM \"gmrml.dtd\">")
        output.write("\n")

        output.write("<GMRML>")
        output.write("\n")

        for counter in xrange(0, self.graph_counter):
            (self.graphs[counter + 1]).write(output)

        output.write("</GMRML>")
        output.write("\n")
